﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;

namespace WorkStation.Frameworks.Core.Data
{
    /// <summary>
    /// * 通用查询规格书
    /// </summary>
    /// <typeparam name="T">参选数据类型</typeparam>
    public class Specification<T> : ISpecification<T>
    {
        private Expression<Func<T, bool>> _isSatisfiedBy;

        /// <summary>
        /// * 构造函数
        /// </summary>
        /// <param name="isSatisfiedBy">初始表达式</param>
        public Specification(Expression<Func<T, bool>> isSatisfiedBy) 
        {
            this._isSatisfiedBy = isSatisfiedBy;
        }

        #region ISpecification<T> Members

        /// <summary>
        /// * 是否符合规格
        /// </summary>
        /// <param name="candidate">参选数据</param>
        /// <returns></returns>
        public bool IsSatisfiedBy(T candidate)
        {
            return this._isSatisfiedBy.Compile()(candidate);
        }

        /// <summary>
        /// * 表达式
        /// </summary>
        public Expression<Func<T, bool>> Expression 
        {
            get { return this._isSatisfiedBy; }
            set { this._isSatisfiedBy = value; }
        }

        #endregion
    }
}
